package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;

	[SWF(width="500",height="300")]
	public class Main extends Sprite
	{
		private var grade:int=100//分数
		private var colorCircle:uint  //宝物类颜色
		public var oCuboid:Cuboid  //控制器
		private var oCircleYellow:Circle;  //宝物类金币
		private var oCircleBlack:Circle;  //宝物类炸弹
		private var oCircleCloseNm:Circle;
		private var a:Boolean=false  // 左
		private var d:Boolean=false  // 右
		private var ctrl:Boolean=false
		private var otext:TextField
		public function Main()
		{
			init()		
		}
		
		//初始化
		private function init():void
		{
		  createText()
		  createCuboid()
		  createCircle()
		}
		//添加文本框
		private function createText():void
		{
			otext=new TextField()
			otext.width=110
			otext.background=true;
			otext.backgroundColor=0x000000
			otext.textColor=0xffffff
			otext.height=20
			otext.border=0xcccccc
			otext.x=otext.y=0
			addChild(otext)
		   haveGrade(grade)
		}
		private function haveGrade(grade):void
		{
			otext.text="分数:"+grade.toString()
		}
		
		private function createCircle():void
		{
			// 画金币圆
			oCircleYellow=new Circle()
			oCircleYellow.setColor=0xffff00;
			addChild(oCircleYellow)
			
			//画炸弹
			oCircleBlack=new Circle()
			oCircleBlack.setColor=0x000000;
			addChild(oCircleBlack)
			
		}
		
		
		//添加显示列表
		private function createCuboid():void
		{
			oCuboid=new Cuboid(0xff00ff)
			oCuboid.x=220
			oCuboid.y=260
			addChild(oCuboid)
			stage.addEventListener(KeyboardEvent.KEY_DOWN,ondown)
			stage.addEventListener(KeyboardEvent.KEY_UP,onup)
			stage.addEventListener(Event.ENTER_FRAME,onFrame)
		}
		
		//判断是否超出边界
		private function ifSuperStage():void
		{
		   if(oCuboid.x<0)
		   {
		     oCuboid.x=0
		   }
		   if(oCuboid.x>=460){
		     oCuboid.x=460
		   }
		}
		//判断是否允许移动
		private function onFrame(e:Event):void
		{
		   if(a)
		  {
			  oCuboid.x-=5;
			  if(ctrl)
			  {
			  oCuboid.x-=10
			  }
		  }
		  if(d)
		  {
			  oCuboid.x+=5		 
			  if(ctrl)
			  {
				  oCuboid.x+=10
			  }   
		  }
		  //碰撞检测
		  ifHit(oCircleYellow,oCuboid)
		  ifHit(oCircleBlack,oCuboid)
		  ifSuperStage()		
		}
		private function ifHit(ifHitOcircle,hitObject):void
		{
			for(var i:int=0;i<ifHitOcircle.CircleArray.length;i++)
			{
				
				//
				if(hitObject.hitTestObject(ifHitOcircle.CircleArray[i]))
				{ 
					if(ifHitOcircle == oCircleYellow)
					{
					grade +=10
					haveGrade(grade)
					}else
					{
						grade -=20
						haveGrade(grade)	
					}
					ifHitOcircle. removeChildAt(i)
					ifHitOcircle.CircleArray.splice(i,1)
					ifHitOcircle.depth--
					oCuboidState()
				}	  
			}
		}
		
		
		//控制器状态
		private function oCuboidState():void
		{
			switch(grade)
			{
				case 90:
					oCuboid.alpha=0.9;
				break;
				case 80:
					oCuboid.alpha=0.8;
					break;
				case 70:
					oCuboid.alpha=0.7;
					break;
				case 60:
					oCuboid.alpha=0.6;
					break;
				case 50:
					oCuboid.alpha=0.5;
					break;
				case 40:
					oCuboid.alpha=0.4;
					break;
				case 30:
					oCuboid.alpha=0.3;
					break;
				case 20:
					oCuboid.alpha=0.2;
					break;
				case 10:
					oCuboid.alpha=0.1;
					break
				case 0:			
					oCuboid.alpha=0;
					otext.text="您好!请及时加冲值!"
					removeChild(oCuboid)
					stage.removeEventListener(KeyboardEvent.KEY_DOWN,ondown)
					stage.removeEventListener(KeyboardEvent.KEY_UP,onup)
					stage.removeEventListener(Event.ENTER_FRAME,onFrame)
					break				
				case -10:			
						oCuboid.alpha=0;
						otext.text="您好!请及时加冲值!"
						removeChild(oCuboid)
						stage.removeEventListener(KeyboardEvent.KEY_DOWN,ondown)
						stage.removeEventListener(KeyboardEvent.KEY_UP,onup)
						stage.removeEventListener(Event.ENTER_FRAME,onFrame)
						break
			}
		}

		
		//按下去触发
		private function ondown(e:KeyboardEvent):void
		{
			trace(e.keyCode)
 			 if(e.keyCode == 65)
			 {
				 
			   a = true
			   twoSpeedDown()
			   twoSpeedUp()
			}
			 if(e.keyCode == 68)
			 {
				 d = true
				 twoSpeedDown()
				 twoSpeedUp()
			 }
		}
		
		//二次判断加速移动
		private function twoSpeedDown():void
		{
			stage.addEventListener(KeyboardEvent.KEY_DOWN,ondownOne)
		}
		private function twoSpeedUp():void
		{
			stage.addEventListener(KeyboardEvent.KEY_UP,onupOne)
		}
		private function ondownOne(e:KeyboardEvent):void
		{
		  if(e.keyCode  == 74)
		  {
			  ctrl=true
		  }
		}		
		private function onupOne(e:KeyboardEvent):void
		{
			if(e.keyCode  == 74)
			{
				ctrl=false
			}
		}
		
		//抬起,触发
		private function onup(e:KeyboardEvent):void
		{
			
			if(e.keyCode == 65 && e.keyCode == 74 )
			{
				a = false
				ctrl = false
			}
			if(e.keyCode == 65)
			{
				a= false
			}
			if(e.keyCode == 68 && e.keyCode == 74)
			{
				d = false
				ctrl = false
			}
			if(e.keyCode == 68)
			{
				d = false
			}
		}
	}
}